import store from '@/store'
import pages from '@/helper/page'
import { buildListToTree } from '@/helper/tools'
import { RouteView, BasicLayout } from '@/components/layouts'
export const constRouter = [
    {
        name: '404',
        path: '/404',
        component: pages['/404.vue']
    },
    {
        name: 'login',
        path: '/login',
        component: pages['/login.vue']
    },
    {
        name: 'regist',
        path: '/regist',
        component: pages['/regist.vue']
    },
    {
        path: '*',
        redirect: '/404'
    }
]

export function creatAsyncRouter (data) {
    var routerArr = []
    data.map(item => {
        routerArr.push(parseRouteItem(item))
    })
    routerArr = buildListToTree(routerArr)
    store.commit('setMenus', routerArr)
    return [{
        path: '/',
        component: BasicLayout,
        redirect: '/woekspace',
        meta: {
            title: '首页'
        },
        children: routerArr
    }]
}

function parseRouteItem (obj) {
    var { id, pid, path, name, redirect, component, title, icon } = obj
    var item = {
        id,
        pid,
        path,
        name,
        redirect,
        component: component ? pages[component] : RouteView,
        meta: { title }
    }
    icon && (item.meta.icon = icon)
    return item
}
